<?php

namespace App\Libs\Company\SheetTemplateHandler\Aggregate;

use App\Libs\Company\SheetTemplateHandler\Helper;
use App\Models\AggregateReport;
use App\Models\SheetTemplate;
use App\Models\Task;

abstract class AbstractTemplate
{
    public $template;

    public $task;

    public $report = null;

    public $output = [];

    public $dataStartRow = 0;

    protected $dataEndRow = 0;

    public function __construct(SheetTemplate $template, Task $task = null)
    {
        $this->template = $template;
        $this->task = $task;
        if (!$this->dataEndRow) {
            $this->dataEndRow = $this->dataStartRow;
        }

        if ($task) {
            $this->report = AggregateReport::firstOrNew([
                'sheet_template_id' => $this->template->id,
                'frequency' => $this->task->frequency,
                'year' => $this->task->year,
                'key' => $this->task->key,
            ]);
        }
    }

    public function renderTemplate()
    {
        $this->renderHeader();
        $this->renderFooter();
        return $this->output;
    }

    protected function renderHeader()
    {
        $this->output = [
            'styles' => $this->template->data['styles'],
            'sheets' => [
                'sheet-01' => [
                    'name' => 'Sheet1',
                    'cellData' => $this->template->data['header'],
                ],
            ],
        ];
        if (isset($this->template->data['mergeData'])) {
            $this->output['sheets']['sheet-01']['mergeData'] = $this->template->data['mergeData'];
        }
        if (isset($this->template->data['rowData'])) {
            $this->output['sheets']['sheet-01']['rowData'] = $this->template->data['rowData'];
        }
        if (isset($this->template->data['columnData'])) {
            $this->output['sheets']['sheet-01']['columnData'] = $this->template->data['columnData'];
        }
    }

    protected function renderFooter()
    {
        $currRow = $this->dataEndRow + 1;
        $footerData = [];
        $footerMergeData = [];
        $footerRowData = [];

        foreach ($this->template->data['footer'] as $key => $row) {
            $footerData[$currRow] = $row;
            if (isset($row['mergeData']['startColumn'])) {
                $footerMergeData[] = array_merge($row['mergeData'], [
                    'startRow' => $currRow,
                    'endRow' => $currRow,
                ]);
            }
            if (isset($row['rowData'])) {
                $footerRowData[$currRow] = $row['rowData'];
            }
            foreach ($footerData[$currRow] as $key => &$cell) {
                if (empty($cell['sum'])) {
                    continue;
                }
                $column = Helper::stringFromColumnIndex($key + 1);
                $cell['f'] = "=SUM({$column}" . $this->dataStartRow + 1 . ":{$column}" . $this->dataEndRow + 1 . ")";
                unset($cell['sum']);
            }
            $currRow++;
        }
        $this->output['sheets']['sheet-01']['cellData'] += $footerData;
        $this->output['sheets']['sheet-01']['mergeData'] = array_merge($this->output['sheets']['sheet-01']['mergeData'], $footerMergeData);
        $this->output['sheets']['sheet-01']['rowData'] += $footerRowData;
    }

    public function getData()
    {
        return [];
    }

    abstract function generateAbstract();

    // 仅支持双行数据
    public function createTitleCell($text)
    {
        $text=trim($text, "\r\n");
        $len = mb_strlen($text);

        return [
            'p' => [
                'id' => 'd',
                'body' => [
                    'textRuns' => [
                        [
                            'ed' => $len,
                            'st' => 0,
                            'ts' => ['fs' => 16]
                        ]
                    ],
                    'dataStream' => $text . "\r\n",
                    'paragraphs' => [
                        ['startIndex' => mb_strpos($text, "\r"), 'paragraphStyle' => ['horizontalAlign' => 2]],
                        ['startIndex' => $len, 'paragraphStyle' => ['horizontalAlign' => 2]]
                    ],
                    'sectionBreaks' => [
                        ['startIndex' => $len + 1]
                    ]
                ],
            ],
            's' => 'TITLE'
        ];
    }
}
